//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Diagnostics;
using System;
using System.IO;
using WinPcap;

namespace SoapTrace.Tests.Logging
{
	/// <summary>
	/// Tests the logging of two data packets and the reading back in of the
	/// data.
	/// </summary>
	[ TestFixture ]
	public class TwoPacketsLoggedTestFixture
	{
		string RemoteServer = "200.1.1.2";
		DateTime FirstPacketTimeReceived;
		DateTime SecondPacketTimeReceived;
		int RemoteServerPort = 8080;
		PacketLogEntryCollection LogEntries;
		PacketLogEntry FirstLogEntry;
		PacketLogEntry SecondLogEntry;
		Packet FirstPacket;
		Packet SecondPacket;
		
		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ SetUp ]
		public void Init( )
		{
			// Create first packet.
			byte[ ] Bytes = new byte[ ] { 0x0, 0x1, 0x2, 0x3 };
			FirstPacket = new Packet( Bytes );
			
			// Create packet log entry.
			FirstPacketTimeReceived = new DateTime( 2000, 2, 2, 5, 15, 30, 60 );
			PacketLogEntry NewLogEntry = new PacketLogEntry( RemoteServer, RemoteServerPort, FirstPacketTimeReceived, FirstPacket, PacketLogEntry.Ethernet );

			PacketLogEntryCollection CreatedLogEntries = new PacketLogEntryCollection( );
			CreatedLogEntries.Add( NewLogEntry );
			
			// Create second packet.
			Bytes = new byte[ ] { 0xA, 0xFF };
			SecondPacket = new Packet( Bytes );
			
			// Create packet log entry.
			SecondPacketTimeReceived = new DateTime( 2001, 3, 4, 6, 22, 35, 23 );
			NewLogEntry = new PacketLogEntry( RemoteServer, RemoteServerPort, SecondPacketTimeReceived, SecondPacket, PacketLogEntry.Ethernet );

			CreatedLogEntries.Add( NewLogEntry );
			
			// Log packet info to file.
			StringWriter StringWriter = new StringWriter( );
			PacketLogFile.Save( StringWriter, CreatedLogEntries );
			
			// Load saved packet info.			
			StringReader StringReader = new StringReader( StringWriter.ToString( ) );
			LogEntries = PacketLogFile.Load( StringReader );
			
			FirstLogEntry = LogEntries[ 0 ];
			SecondLogEntry = LogEntries[ 1 ];
		}
		
		/// <summary>
		/// Check that we have got 2 entries.
		/// </summary>
		[ Test ]
		public void TwoEntries( )
		{
			Assert.AreEqual( 2, LogEntries.Count, "Should be 2 log entries." );
		}
		
		/// <summary>
		/// Checks the packet has been read back in correctly.
		/// </summary>
		[ Test ]
		public void FirstLogEntryPacketByteLength( )
		{
			byte[ ] PacketBytes = FirstLogEntry.Packet.GetBytes( );
			byte[ ] ExpectedBytes = FirstPacket.GetBytes( );
			
			Assert.AreEqual( ExpectedBytes.Length, PacketBytes.Length, "Number of packet bytes differ." );
		}
		
		/// <summary>
		/// Checks the packet has been read back in correctly.
		/// </summary>
		[ Test ]
		public void FirstLogEntryPacketBytes( )
		{
			byte[ ] PacketBytes = FirstLogEntry.Packet.GetBytes( );
			byte[ ] ExpectedBytes = FirstPacket.GetBytes( );
			
			for( int i = 0; i < ExpectedBytes.Length; ++i )
			{
				byte ActualByte = PacketBytes[ i ];
				byte ExpectedByte = ExpectedBytes[ i ];
				
				Assert.AreEqual( ExpectedByte, ActualByte, 
				                String.Concat( "Byte ", i.ToString( ), " in the packet is incorrect." ) );
			}	
		}
		
		/// <summary>
		/// Checks the packet has been read back in correctly.
		/// </summary>
		[ Test ]
		public void SecondLogEntryPacketByteLength( )
		{
			byte[ ] PacketBytes = SecondLogEntry.Packet.GetBytes( );
			byte[ ] ExpectedBytes = SecondPacket.GetBytes( );
			
			Assert.AreEqual( ExpectedBytes.Length, PacketBytes.Length, "Number of packet bytes differ." );
		}
		
		/// <summary>
		/// Checks the packet has been read back in correctly.
		/// </summary>
		[ Test ]
		public void SecondLogEntryPacketBytes( )
		{
			byte[ ] PacketBytes = SecondLogEntry.Packet.GetBytes( );
			byte[ ] ExpectedBytes = SecondPacket.GetBytes( );
			
			for( int i = 0; i < ExpectedBytes.Length; ++i )
			{
				byte ActualByte = PacketBytes[ i ];
				byte ExpectedByte = ExpectedBytes[ i ];
				
				Assert.AreEqual( ExpectedByte, ActualByte, 
				                String.Concat( "Byte ", i.ToString( ), " in the packet is incorrect." ) );
			}	
		}		
	}
}
